# GUIdemo11.py
# Demo11 of GUI by PyQt5
# Copyright 2021 youcans, XUPT
# Crated：2021-10-20

import sys, math,os

from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtCore import pyqtSignal, QThread, QFileInfo, QAbstractListModel, QModelIndex, QVariant, Qt
from PyQt5.Qt import *
from PyQt5.QtGui import QStandardItemModel, QStandardItem, QBrush, QColor

from PyQt5.QtWidgets import QApplication, QMainWindow, QMessageBox, QFileSystemModel, QTreeWidgetItem, QFileIconProvider,QDirModel
from MainWindow import Ui_MainWindow  # 导入 uiDemo9.py 中的 Ui_MainWindow 界面类
import json, time
from random import randint
from multiprocessing import Process, Queue

from queue import Queue



# 继承QThread
class Runthread(QtCore.QThread):
    # 通过类成员对象定义信号对象
    _signal = pyqtSignal(str)

    def __init__(self, index=0, money=0.5, msg=""):
        super(Runthread, self).__init__()
        print("传入的参数", index)
        self.index = index
        self.money = money
        self.msg = msg

    def __del__(self):
        self.wait()

    def run(self):
        self.openWindows(index=self.index, money=self.money, msgStr=self.msg)
        self._signal.emit(0)  # 注意这里与_signal = pyqtSignal(str)中的类型相同

    def openWindows(self, index, money, msgStr):
        print("人民币", money)

class MyMainWindow(QMainWindow, Ui_MainWindow):  # 继承 QMainWindow 类和 Ui_MainWindow 界面类
    def __init__(self, parent=None):
        super(MyMainWindow, self).__init__(parent)  # 初始化父类
        self.setupUi(self)  # 继承 Ui_MainWindow 界面类


        # 获取系统所有文件
        self.model01 = QFileSystemModel()
        # 进行筛选只显示文件夹，不显示文件和特色文件
        self.model01.setFilter(QtCore.QDir.Dirs | QtCore.QDir.NoDotAndDotDot)
        self.model01.setRootPath("")

        self.treeView_local_L.setModel(self.model01)
        for col in range(1, 4):
            self.treeView_local_L.setColumnHidden(col,True)
        self.treeView_local_L.doubleClicked.connect(self.initUI)
        self.treeView_local_L.allColumnsShowFocus()

        self.model02 = QStandardItemModel()
        self.listView_local_R.setModel(self.model02)

        # 获取系统所有文件
        self.model_ftp_01 = QFileSystemModel()
        # 进行筛选只显示文件夹，不显示文件和特色文件
        self.model_ftp_01.setFilter(QtCore.QDir.Dirs | QtCore.QDir.NoDotAndDotDot)
        self.model_ftp_01.setRootPath("")

        self.treeView_server_L.setModel(self.model01)
        for col in range(1, 4):
            self.treeView_server_L.setColumnHidden(col, True)
        self.treeView_server_L.doubleClicked.connect(self.initFTP)
        self.treeView_server_L.allColumnsShowFocus()

        self.model_ftp_02 = QStandardItemModel()
        self.listView_server_R.setModel(self.model_ftp_02)

        self.pushButton_push_manhua.clicked.connect(self.update_manhua)

    def update_manhua(self):
        print("上传漫画")




    def initFTP(self,Qmodelidx):
        # QtWidgets.QFileSystemModel.data()
        print('当前选中的项目', Qmodelidx.data())
        # 每次点击清空右边窗口数据
        self.model_ftp_02.clear()
        # 定义一个数组存储路径下的所有文件
        PathData = []
        # 获取双击后的指定路径
        filePath = self.model_ftp_01.filePath(Qmodelidx)
        print("获取双击后的指定路径", filePath)
        # List窗口文件赋值
        PathDataName = self.model_ftp_02.invisibleRootItem()
        # 拿到文件夹下的所有文件
        PathDataSet = os.listdir(filePath)
        # 进行将拿到的数据进行排序
        PathDataSet.sort()
        # 遍历判断拿到的文件是文件夹还是文件，Flase为文件，True为文件夹
        for Data in range(len(PathDataSet)):
            if os.path.isdir(filePath + '\\' + PathDataSet[Data]) == False:
                PathData.append(PathDataSet[Data])
            elif os.path.isdir(filePath + '\\' + PathDataSet[Data]) == True:
                print(filePath + '\\' + PathDataSet[Data])
        # 将拿到的所有文件放到数组中进行右边窗口赋值。
        for i in range(len(PathData)):
            gosData = QStandardItem(PathData[i])
            PathDataName.setChild(i, gosData)

    def initUI(self, Qmodelidx):
        # QtWidgets.QFileSystemModel.data()
        print('当前选中的项目',Qmodelidx.data())
        # 每次点击清空右边窗口数据
        self.model02.clear()
        # 定义一个数组存储路径下的所有文件
        PathData = []
        # 获取双击后的指定路径
        filePath = self.model01.filePath(Qmodelidx)
        print("获取双击后的指定路径",filePath)
        # List窗口文件赋值
        PathDataName = self.model02.invisibleRootItem()
        # 拿到文件夹下的所有文件
        PathDataSet = os.listdir(filePath)
        # 进行将拿到的数据进行排序
        PathDataSet.sort()
        # 遍历判断拿到的文件是文件夹还是文件，Flase为文件，True为文件夹
        for Data in range(len(PathDataSet)):
            if os.path.isdir(filePath + '\\' + PathDataSet[Data]) == False:
                PathData.append(PathDataSet[Data])
            elif os.path.isdir(filePath + '\\' + PathDataSet[Data]) == True:
                print(filePath + '\\' + PathDataSet[Data])
        # 将拿到的所有文件放到数组中进行右边窗口赋值。
        for i in range(len(PathData)):
            gosData = QStandardItem(PathData[i])
            PathDataName.setChild(i, gosData)

    def save(self):
        print("save")
        pass
        je = self.lineEdit_je.text()
        print(je)
        conten = self.textEdit_content.toMarkdown()
        print(conten)
        with open(r".\save.json", "w") as f:
            data = {}
            data['je'] = je
            data['content'] = conten
            data = json.dumps(data)
            f.write(data)

    def read(self):
        print("read")
        print("read")
        with open(r".\save.json", "r") as f:
            content = f.read()
            try:
                data = json.loads(content)
                self.lineEdit_je.setText(data['je'])
                self.textEdit_content.setMarkdown(data['content'])
            except:
                pass

    def start_login(self, index):
        # 创建线程
        money = float(self.lineEdit_je.text())
        msg = str(self.textEdit_content.toMarkdown())
        self.thread = Runthread(index=index, money=money, msg=msg)
        # 连接信号
        self.thread._signal.connect(self.call_backlog)  # 进程连接回传到GUI的事件
        # 开始线程
        self.thread.start()

    def call_backlog(self, msg):
        print("线程返回的参数", msg)
        # self.pbar.setValue(int(msg))  # 将线程的参数传入进度条

if __name__ == '__main__':  # youcans, XUPT 2021
    app = QApplication(sys.argv)  # 在 QApplication 方法中使用，创建应用程序对象
    myWin = MyMainWindow()  # 实例化 MyMainWindow 类，创建主窗口
    myWin.show()  # 在桌面显示控件 myWin
    sys.exit(app.exec_())  # 结束进程，退出程序
